package jfae.core.properties.impl;

import java.util.Arrays;
import java.util.Collection;

import javax.persistence.Entity;

import jfae.core.annotations.Editable;
import jfae.core.annotations.Presentation;
import jfae.core.properties.FieldInfo;


/**
 * Rappresentazione base di una proprietà di un entity
 * @author bobpuley
 *
 * @param <T>
 */
public class FieldInfoImpl<T> implements FieldInfo<T>{
	
	private String name;
	private String label;
	private String format;
	private String[] groups;
	private Collection<String> cgroups;
	private Class<T> type;
	private int order;
	private int columnSize;
	private int fieldSize;
	private int position;
	private String styles;
	private String styleClass;
	private boolean time;
	private boolean required;
	private int fieldLength = 0;
	
	public FieldInfoImpl() {
		super();
		this.label = Presentation.NO_LABEL_SET;
		this.groups = new String[]{Editable.NO_GROUP_SET};
		this.cgroups = Arrays.asList(this.groups);
		this.order = Editable.DEFAULT_ORDER;
		this.columnSize = Presentation.DEFAULT_COLUMN_SIZE;
		this.fieldSize = Presentation.DEFAULT_FIELD_SIZE;
		this.time = false;
		this.required = false;
		this.fieldLength = Presentation.DEFAULT_FIELD_LENGTH;
	}

	public FieldInfoImpl(String name, Class<T> type) {
		this();
		this.name = name;
		this.type = type;
	}

	protected FieldInfoImpl(FieldInfo<T> source) {
		this(source.getName(), source.getType());
		setGroups(source.getGroups());
		setOrder(source.getOrder());
		setLabel(source.getLabel());
		setFormat(source.getFormat());
		setTime(source.isTime());
		setColumnSize(source.getColumnSize());
		setFieldSize(source.getFieldSize());
		setStyles(source.getStyles());
		setStyleClass(source.getStyleClass());
		setRequired(source.isRequired());
		setFieldLength(source.getFieldLength());
	}
	
	
	public String getName() {
		return name;
	}
	
	public Class<T> getType() {
		return type;
	}

	public int getOrder() {
		return order;
	}
	public void setName(String name) {
		this.name = name;
	}

	public void setType(Class<T> type) {
		this.type = type;
	}

	public void setOrder(int order) {
		this.order = order;
	}
	
	public boolean isEnum(){
		return type.isEnum();
	}

	public boolean isEntity(){
		return type.isAnnotationPresent(Entity.class);
	}

	public boolean isRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public String getLabel() {
		return !Presentation.NO_LABEL_SET.equals(label) ? label : name;
	}

	public void setLabel(String label) {
		this.label = label;
	}
	
	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public boolean isTime() {
		return time;
	}
	
	public void setTime(boolean time) {
		this.time = time;
	}
	
	public String[] getGroups() {
		return groups;
	}

	public void setGroups(String[] groups) {
		this.groups = groups;
		cgroups = Arrays.asList(groups);
	}
	
	public int getFieldSize() {
		return fieldSize;
	}
	
	public int getColumnSize() {
		return columnSize;
	}
	
	public boolean isInGroup(String group){
		return this.cgroups != null && this.cgroups.contains(group);
	}
	
	public String getStyles() {
		return styles;
	}
	public void setStyles(String styles) {
		this.styles = styles;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public void setColumnSize(int columnSize) {
		this.columnSize = columnSize;
	}

	public void setFieldSize(int fieldSize) {
		this.fieldSize = fieldSize;
	}

	public int getFieldLength() {
		return fieldLength;
	}

	public void setFieldLength(int fieldLength) {
		this.fieldLength = fieldLength;
	}
	
	public String getIdentifier(){
		return name;
	}
	
	public String getPropertyName(){
		return name;
	}

	public String getExpression(){
		return name;
	}
	
	@Override
	public String toString() {
		return String.format("%s [%s - %s]", getClass().getSimpleName(), name, type);
	}
	
	public FieldInfoImpl<T> clone(){
		return new FieldInfoImpl<T>(this);
	}
	
}